import { orderCancel, cancelOrderRefund, orderConfirmReceiving, prepaymentPay } from "../api/order";

// max: 21
const orderBtns = [
  {
    id: 1,
    name: '立即付款',
    status: [3]
  },
  {
    id: 99,
    name: '继续提交',
    status: [2]
  },
  {
    id: 2,
    name: '取消订单',
    status: [2,4]
  },
  {
    id: 3,
    name: '确认收货',
    status: [20]
  },
  {
    id: 4,
    name: '立即还款',
    status: [30,38,39]
  },
  {
    id: 5,
    name: '取消归还',
    status: [41, 43]
  },
  {
    id: 6,
    name: '查看归还物流',
    status: [44]
  },
  {
    id: 7,
    name: '再次下单',
    status: [45, 62, 60, 1, 13, 61]
  },
  {
    id: 8,
    name: '取消续租',
    status: [50, 51]
  },
  {
    id: 9,
    name: '申请退款',
    status: [9, 10, 13]
  },
  {
    id: 10,
    name: '申请归还',
    status: [30, 39]
  },
  {
    id: 11,
    name: '添加物流',
    status: [40, 42]
  },
  {
    id: 12,
    name: '再次申请',
    status: [43, 52]
  },
  {
    id: 13,
    name: '立即下单',
    status: [51]
  },
  {
    id: 14,
    name: '申请买断',
    status: [30, 39, 40]
  },
  {
    id: 15,
    name: '申请续租',
    status: [40]
  },
  {
    id: 16,
    name: '归还地址',
    status: [40, 42]
  },
  {
    id: 18,
    name: '取消退款',
    status: [11, 13]
  },
  {
    id: 19,
    name: '查看物流',
    status: [20, 30, 32, 39]
  },
  {
    id: 20,
    name: '签署合同',
    // status: [3, 9, 10, 20]
    status: [3]
  },
  {
    id: 22,
    name: '重新签约',
    status: [10, 20, 30, 39, 40]
  },
  {
    id: 17,
    name: '联系客服',
    status: [10, 11, 20, 30, 39, 40, 41, 42, 43, 44, 45, 50, 51, 52, 62, 60, 1, 61]
  },
  {
    id: 30,
    name: '签署租赁宝',
    status: [3]
  },
  {
    id: 35,
    name: '芝麻免押',
    status: [2,3,4,10]
  },
  // {
  //   id: 31,
  //   name: '协议公证',
  //   status: [3]
  // },
]
function getBtnsByStatus(res) {
  const { status: order_status, sign_status, order_from, pay_type, pact_type, member_id, is_show,withhold_status,is_notary ,rights_auth_no } = res;
  let result = [];
  if (order_from === 'qnhlapp') {
    // 判断千年国医
    const hideList = [2, 8, 9, 18, 14, 15];
    result = orderBtns.filter(item => item.status.indexOf(order_status) > -1 && hideList.indexOf(item.id) === -1);
  } else {
    result = orderBtns.filter(item => item.status.indexOf(order_status) > -1);
  }

  // 有签署合同按钮判断是否签约完成
  const index = result.findIndex(k => k.id == 20);
  if (index > -1 && sign_status == 2) {
    result.splice(index, 1);
  }

  const index30 = result.findIndex(k => k.id == 30);


  if (index30>-1&&(withhold_status==1||sign_status != 2)) {
    result.splice(index30, 1);
  }
  
  const index35 = result.findIndex(k => k.id == 35);


  if (index35>-1&&rights_auth_no) {
    result.splice(index35, 1);
  }

  const index14 = result.findIndex(k => k.id == 14);


  //非买断金支付
  if (index14>-1&&pact_type == 2) {
    result.splice(index14, 1);
  }

  // 判断签约未支付是否可以付款
  // const index2 = result.findIndex(k => k.id == 1);

  const index2 = result.findIndex(k => k.id == 1);

  if (index2>-1&&(withhold_status!=1||sign_status != 2 )) {
    result.splice(index2, 1);
  }
  
    // const index31 = result.findIndex(k => k.id == 31);
    // if (index31>-1&&(sign_status != 2)) {
    //     result.splice(index31, 1);
    // }

  // if (is_notary!=1) {
  //   result.splice(index2, 1);
  // }

  // 判断是否显示归还相关按钮(租完即送)
  const index3 = result.findIndex(k => k.id == 5 || k.id == 6 || k.id == 10 || k.id == 11 || k.id == 16);
  if (index3 > -1 && pact_type == 2) {
    result.splice(index3, 1);
  }
  // 判断是否显示重新签约按钮
  const index4 = result.findIndex(k => k.id == 22);
  // if (index4 > -1 && (pay_type != 1 || member_id != 1601436707) ) {
  //if (index4 > -1 && pay_type != 1) {
  if (index4 > -1) {
    result.splice(index4, 1);
  }
  // 隐藏再次下单
  const index5 = result.findIndex(k => k.id == 7);
  if (index5 > -1 && is_show != 1) {
    result.splice(index5, 1);
  }
  return result;
}

// 未支付取消订单
function cancelOrder(order_id, callback) {
  orderCancel(order_id).then(res => {
    my.showToast({
      type: 'success',
      content: '操作成功',
      duration: 3000
    });
    callback();
  })
}

// 撤销退款申请
function cancelRefundApply(order_id, callback) {
  cancelOrderRefund(order_id).then(res => {
    my.showToast({
      type: 'success',
      content: '操作成功',
      duration: 3000
    });
    callback();
  })
}

// 确认收货
function confirmReceipt(order_id, callback) {
  orderConfirmReceiving(order_id).then(res => {
    my.showToast({
      type: 'success',
      content: '操作成功',
      duration: 3000
    });
    callback();
  })
}

// 签署成功主动支付第一期
function payFirstRent(orderId, callback) {
  prepaymentPay({ orderId, leaseId: '' }).then(res => {
    if (res.tradeNo) {
      my.tradePay({
        tradeNO: res.tradeNo,
        success: (res) => {
          if (res.resultCode == 9000) {
            callback();
          } else {
            my.showToast({
              type: 'fail',
              content: '支付失败，请稍后重试',
              duration: 1000,
            });
          }
        }
      });
    }
  })
}

module.exports = { getBtnsByStatus, cancelOrder, cancelRefundApply, confirmReceipt, payFirstRent }